/**
 * Created by henandeyi on 2017/7/17.
 */
var formidable = require('formidable');
var db = require('../models/db.js');
var MD5 = require('../models/md5.js');
var path = require('path');
var fs = require('fs');

exports.showIndex = function(req,res){

    res.render('index',{
        'login':req.session.login == '1' ? true : false,
        'username':req.session.login == '1' ? req.session.username :''
    });

};
exports.shouRegist = function(req,res,next){

    res.render('regist');

};
exports.doregist = function(req,res,next){

    var form = new formidable.IncomingForm();

    form.parse(req, function(err, fields) {

        var username = fields.username;
        var password = fields.password;
        db.find('user',{'username':username},function (err,result) {
            if(err){
                res.send('-3');
                return;
            }
            if(result.length!=0){
                res.send('-1');
                return;
            }
            db.insertOne('user',{
                'username':username,
                'password':MD5(password),
                'avatar':'moren.jpeg'
            },function (err, result) {
                if(err){
                    res.send('-3');
                    return;
                }
                req.session.login = '1';
                req.session.username = username;
                res.send('1');
            });
        });

    });

};
exports.showlogin = function(req,res,next){

    res.render('login');

};
exports.showsetavatar = function(req,res,next){

    res.render('setavatar');

};
exports.showmyfabu = function(req,res,next){

    res.render('myfabu');

};
exports.dologin = function(req,res,next){

    var form = new formidable.IncomingForm();

    form.parse(req, function(err, fields) {


        var username = fields.username;
        var password = fields.password;
        db.find('user',{'username':username},function (err,result) {
            if(err){
                res.send('-3');
                return;
            }
            if(result.length == 0){
                res.send('-1');
                return;
            }
            var obj = result[0];
            if(obj.password == MD5(password)){
                console.log('登录成功');
                req.session.login = '1';
                req.session.username = username;
                res.send('1');
            }else{
                //密码错误
                console.log('密码错误');
                res.send('-4');
                return;
            }
        });

    });

};
exports.dosetavatar = function(req,res){


    if(!req.session.username){
        res.send('您没有登录');
        return;
    }

    var form = new formidable.IncomingForm();
    var imgpath = path.normalize(__dirname+'/../avatar');
    form.uploadDir = imgpath;

    form.parse(req, function(err, fields,files) {

        var oldpath = files.setavatar.path;
        var newpath = path.normalize(__dirname+'/../avatar')+'/'+req.session.username+'.jpg';
        var username =  req.session.username;
        var imgname = username + '.jpg';
        fs.rename(oldpath,newpath,function (err,result) {
            if(err){
                res.send('修改失败');
                return;
            }
            //修改数据库里对应的头像图片名字
            db.updateMany('user',{'username':username},{$set: { 'avatar':imgname }},function (err, data) {
                if(err){
                    console.log('修改头像失败');
                    return;
                }
                console.log('修改成功'+data);
            });
            res.redirect('/');
        });

    });
};
exports.dopostcontent = function (req,res) {

    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields) {

        var content = fields.content;

        var date = new Date();
        var username = req.session.username;
        console.log(fields+username);
        db.find('user',{'username':username},function (err,result) {
            if(err){
                res.send('-3');
                return;
            }
            if(result.length == 0){
                res.send('-1');
                return;
            }
            var obj = result[0];
            db.insertOne('content',{
                'username':username,
                'content':content,
                'date':date,
                'avatar':'/avatar/'+obj.avatar
            },function (err, result) {
                if(err){
                    res.send('-3');
                    return;
                }
                res.json('1');
                console.log('插入发布内容成功');
            });

        });
    });
};
exports.getcontentlist = function (req,res) {

    db.find('content',{},{'sort':{'date':-1}},function (err,result){
        if(err){
            res.send('-3');
            return;
        }
        res.json({'result':result});
    });

};
exports.getmyfabu = function (req,res){

    var username = req.session.username;
    db.find('content',{'username':username},{'sort':{'date':-1}},function (err,result){
        if(err){
            res.send('-3');
            return;
        }
        res.json({'result':result});
    });

};